﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CIIS.Common;
using DevExpress.XtraGrid.Views.Base;

namespace CIIS
{
    public partial class RoleManage : BaseUserControl
    {
        public RoleManage()
        {
            InitializeComponent();

           DevControlUtils.Instance.InitRepositoryItemLookUpEditByEnum(repositoryItemLookUpEdit1, typeof(RoleType));
         //   DevControlUtils.Instance.InitLookUpEditByEnum(repositoryItemLookUpEdit1,typeof(RoleType));
            LoadData();
            gridView1.FocusedRowChanged += role_FocusedRowChanged;
        }
        private bool isReload = false;
        public void LoadData()
        {
            if (!isReload)
            {
                this.gridControl1.DataSource = DataUtils.Instance.GetListFromCache<Contract.Domain.t_sys_role>();
            }
            else
            {
                this.gridControl1.DataSource = DataUtils.Instance.ReloadDataList<Contract.Domain.t_sys_role>();
            }
            gridControl1.RefreshDataSource();
        }
        Contract.Domain.t_sys_role role = null;
        void role_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle >= 0)
            {
                role = this.gridView1.GetRow(e.FocusedRowHandle) as Contract.Domain.t_sys_role;
            }
            else
            {
                role = null;

            }
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            if (role != null)
            {
                RoleEdit edit = new RoleEdit(role);
                if (edit.ShowDialog() == DialogResult.OK)
                {
                    LoadData();
                }
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            RoleEdit edit = new RoleEdit();
            if (edit.ShowDialog() == DialogResult.OK)
            {
                isReload = true;
                LoadData();
            }
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            if (role != null)
            {

                if (MessageBox.Show("删除提示", "确认要删除", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    role.State.MarkDeleted();
                    dao.Dao.SaveOrUpdate(role);
                    isReload = true;
                    LoadData();
                }
            }
            else
            {
                MessageBox.Show("请选择要删除的记录！");
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (role != null)
            {
                RoleUser roleuser = new RoleUser(role);
                roleuser.ShowDialog();
            }
        }

    }
}
